<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\ClientAccountModel;
use app\model\mongo\MongoQqAccountDayOldReportModel;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;

class QqAccountOldCostMongoWork extends BaseWork
{
    public const REDIS_TASK_KEY = 'task:qq_old_cost_mongo:list';

    protected function configure()
    {
        // 指令配置
        $this->setName('qq_account_old_cost_mongo');
        $this->setDescription('qq账户老消耗mongo');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======开始' . date('Y-m-d H:i:s', time()) . '=======');
        try {
            $redis = Cache::store('redis')->handler();
            //                                    $accounts = ClientAccountModel::where('media_id', 5)->column('id');
            //                                    foreach ($accounts as $account) {
            //                                        $redis->rPush(self::REDIS_TASK_KEY, $account);
            //                                    }
            //                                    echo 'ok';
            //                                    exit;
            $st = time() + 5 * 60;
            while (time() < $st) {
                $accountId = $redis->lPop(self::REDIS_TASK_KEY);
                //                $accountId = 1952;
                if (! $accountId) {
                    usleep(500000);
                    continue;
                }
                $accountId = intval($accountId);
                $advertiser = ClientAccountModel::where('id', $accountId)->find();
                if (! $advertiser) {
                    continue;
                }
                $advertiserId = $advertiser['advertiser_id'];
                $where = [
                    ['advertiser_id', '=', $advertiserId],
                    ['stat_date', '<=', '2023-03-01'],
                ];
                $datas = Db::name('qq_advertiser_daily_data')->where($where)->select()->toArray();
                foreach ($datas as $data) {
                    $fields = ['total_cost', 'cash_cost', 'cost', 'virtual_gold', 'credit_roll_cost', 'total_balance', 'give_cost', 'turnover_cost', 'return_goods_cost'];
                    $flag = false;
                    foreach ($fields as $field) {
                        if ($data[$field] > 0) {
                            $flag = true;
                            break;
                        }
                    }
                    if (! $flag) {
                        continue;
                    }
                    // 判断是否已存在
                    $statDate = intval(date('Ymd', strtotime($data['stat_date'])));
                    $isHas = (new MongoQqAccountDayOldReportModel())->where('account_id', $accountId)
                        ->where('adv_id', $advertiserId)
                        ->where('stat_date', $statDate)
                        ->find();
                    if (! $isHas) {
                        $mongoData = [
                            'company_id' => $advertiser['company_id'],
                            'client_id' => $advertiser['client_id'],
                            'account_id' => $accountId,
                            'adv_id' => $data['advertiser_id'],
                            'stat_date' => $statDate,
                            'total_cost' => floatval($data['total_cost']),
                            'cost' => floatval($data['cost']),
                            'cash_cost' => floatval($data['cash_cost']),
                            'virtual_gold' => floatval($data['virtual_gold']),
                            'credit_roll_cost' => floatval($data['credit_roll_cost']),
                            'remarks' => $data['remarks'],
                            'total_balance' => floatval($data['total_balance']),
                            'give_cost' => floatval($data['give_cost']),
                            'turnover_cost' => floatval($data['turnover_cost']),
                            'return_goods_cost' => floatval($data['return_goods_cost']),
                            'project_id' => 0,
                            'staff_id' => 0,
                            'is_sync' => 0,
                            'is_real_sync' => 0,
                            'create_time' => time(),
                            'update_time' => time(),
                            'content' => [],
                        ];
                        (new MongoQqAccountDayOldReportModel())->insert($mongoData);
                    }
                }
                echo $accountId . ' ok' . PHP_EOL;
            }
        } catch (\Exception $e) {
            dump($e);
            $output->writeln('=======失败' . $e->getMessage() . date('Y-m-d H:i:s', time()) . '=======');
        }
        $output->writeln('=======结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
